package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.Plan;
import com.efast.dmom.aps.dao.PlanDao;
import com.efast.dmom.aps.entity.PlanEntity;
import com.efast.dmom.aps.util.TimeUtil;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;

@Component
public class PlanDataHandler {
	
	
	@Autowired
	private PlanDao planDao;
	public void save(Plan plan) {
		
		plan.getInnerEntity().setSiteId(DataHandlerUtils.getSiteId());
		plan.getInnerEntity().setCreationTime(TimeUtil.getLocalDateTime());
		plan.getInnerEntity().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		plan.getInnerEntity().setCreator(SpringWebUtils.getRequestUser());
		plan.getInnerEntity().setLastModifiedTime(TimeUtil.getLocalDateTime());
		plan.getInnerEntity().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		plan.getInnerEntity().setLastModifier(SpringWebUtils.getRequestUser());
//		plan.getInnerEntity().setCreationPid(dmomIdDao.getDmomPid());
//		plan.getInnerEntity().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		plan.getInnerEntity().setRowStatus(1);
		plan.getInnerEntity().setRowTrxId(1);
		planDao.save(plan.getInnerEntity());
	}
	public void delete(Plan plan) {
		plan.getInnerEntity().setLastModifiedTime(TimeUtil.getLocalDateTime());
		plan.getInnerEntity().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		plan.getInnerEntity().setLastModifier(SpringWebUtils.getRequestUser());
//		plan.getInnerEntity().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		plan.getInnerEntity().setRowTrxId(1);
		plan.getInnerEntity().setRowStatus(0);
		planDao.delete(plan.getInnerEntity());
		
	}
	public void update(Plan plan) {
		plan.getInnerEntity().setLastModifiedTime(TimeUtil.getLocalDateTime());
		plan.getInnerEntity().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		plan.getInnerEntity().setLastModifier(SpringWebUtils.getRequestUser());
//		plan.getInnerEntity().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		plan.getInnerEntity().setRowTrxId(1);
		plan.getInnerEntity().setRowStatus(1);
		planDao.update(plan.getInnerEntity());
	}
	public Plan getById(long id) {
		PlanEntity byId = planDao.getById(id);
		Plan plan = new Plan();
		plan.setInnerEntity(byId);
		return plan;
	}
	public Plan getByName(String name) {
		PlanEntity byName = planDao.getByName(name);
		Plan plan = new Plan();
		plan.setInnerEntity(byName);
		return plan;
	}
	public List<Plan> getByStatus(String status){
		List<PlanEntity> byStatus = planDao.getByStatus(status);
		List<Plan> plans = new ArrayList<Plan>();
		for(PlanEntity planEntity:byStatus) {
			Plan plan = new Plan();
			plan.setInnerEntity(planEntity);
			plans.add(plan);
		}
		return plans;
	}
	
	public Plan getByPlanNo(String planNo) {
		PlanEntity byPlanNo = planDao.getByPlanNo(planNo);
		Plan plan = new Plan();
		plan.setInnerEntity(byPlanNo);
		return  plan;
	}
}
